package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.Supplier;
import com.yc.supermarket.mapper.SupplierMapper;
import com.yc.supermarket.service.SupplierService;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SupplierServiceImpl implements SupplierService {

    private SupplierMapper mapper;

    @Override
    public Map<String, Object> findByTrem(Supplier supplier) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(SupplierMapper.class);
        List<Supplier> list = mapper.findByTrem(supplier);
        //创建hashmap存储数据
        Map<String, Object> m = new HashMap<>();
        if (null == list ) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public boolean add(Supplier supplier) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(SupplierMapper.class);
        boolean result = mapper.add(supplier);
        return result;
    }

    @Override
    public boolean update(Supplier supplier) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(SupplierMapper.class);
        boolean result = mapper.update(supplier);
        return result;
    }
}
